package com.yhp.controller;

import com.yhp.bean.*;
import com.yhp.mvc.ResponseBody;
import com.yhp.service.impl.CourierService;
import com.yhp.service.impl.UserService;
import com.yhp.util.DateFormatUtil;
import com.yhp.util.GsonUtil;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

public class UserContorller {

    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response) {
        String offset = request.getParameter("offset");
        int offset1 = Integer.parseInt(offset);
        String pageNumber = request.getParameter("pageNumber");
        int pageNumber1 = Integer.parseInt(pageNumber);
        List<User> all = UserService.findAll(true, offset1, pageNumber1);
        List<BootStrapUser> list = new ArrayList<>();
        for (User user : all) {
            String password = user.getPassWord() == null ? "" : "********";
            String inTime = DateFormatUtil.format(user.getInTime());
            String time = user.getTime() == null ? null : DateFormatUtil.format(user.getTime());
            BootStrapUser bootStrapUser = new BootStrapUser(user.getId(), user.getName(), user.getPhone(), password, inTime, time);
            list.add(bootStrapUser);
        }
        int total = UserService.total();
        ResultData<BootStrapUser> data = new ResultData<>();
        data.setRows(list);
        data.setTotal(total);
        String s = GsonUtil.toJSON(data);
        return s;
    }


    @ResponseBody("/user/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String numberId = request.getParameter("numberId");
        String password = request.getParameter("password");
        User u = new User();
        u.setNumberId(numberId);
        u.setName(name);
        u.setPhone(phone);
        u.setPassWord(password);
        boolean insert = UserService.insert(u);
        Message msg = new Message();
        if (insert) {
            msg.setResult("添加成功");
            msg.setStatus(0);
        } else {
            msg.setResult("添加失败");
            msg.setStatus(-1);
        }
        String s = GsonUtil.toJSON(msg);
        return s;

    }

    @ResponseBody("/user/findPhone.do")
    public String findByPhone(HttpServletRequest request, HttpServletResponse response) {
        String findPhone = request.getParameter("findPhone");
        User byPhone = UserService.findByPhone(findPhone);
        Message msg = new Message();
        if (byPhone != null) {
            msg.setStatus(0);
            msg.setResult("查找成功");
            msg.setData(byPhone);
        } else {
            msg.setStatus(-1);
            msg.setResult("查找失败");
        }
        String s = GsonUtil.toJSON(msg);
        return s;
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String numberId = request.getParameter("numberId");
        String passWord = request.getParameter("passWord");
        User u = new User();
        u.setName(name);
        u.setPhone(phone);
        u.setNumberId(numberId);
        u.setPassWord(passWord);
        boolean update = UserService.update(Integer.parseInt(id), u);
        Message msg = new Message();
        if (update) {
            msg.setStatus(0);
            msg.setResult("修改成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String s = GsonUtil.toJSON(msg);

        return s;
    }
    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        int cid = Integer.parseInt(id);
        boolean delete = UserService.delete(cid);
        Message msg = new Message();
        if(delete){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }

        String s = GsonUtil.toJSON(msg);
        return s;
    }
}
